package com.trainfinder;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class BookingAction extends Action {

	// private final static Log LOG = LogFactory.getLog(BookingAction.class);

	private static final SimpleDateFormat DATE = new SimpleDateFormat(
			"dd'%2F'MM'%2F'yyyy");
	 

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		BookingActionForm bookingActionForm = (BookingActionForm) form;

		SimpleBookingActionForm result = new SimpleBookingActionForm();
		result.setDepartureCity(bookingActionForm.getDepartureCity());
		result.setOutwardDate(bookingActionForm.getOutwardDate());
		result.setReturnDate(bookingActionForm.getReturnDate());
		result.setArrivalCity(bookingActionForm.getArrivalCity());
		result.setOutwardTime(bookingActionForm.getOutwardTime());
		result.setReturnDate(bookingActionForm.getReturnDate());
		result.setReturnTime(bookingActionForm.getReturnTime());
		result.setNbPaxAdult(bookingActionForm.getNbPaxAdult());
		result.setNbPaxChild(bookingActionForm.getNbPaxChild());
		result.setNbPaxYouth(bookingActionForm.getNbPaxYouth());
		result.setNbPaxSenior(bookingActionForm.getNbPaxSenior());
		System.out.println(result);
		try {
			Date returnDate = DATE.parse(bookingActionForm.getReturnDate());
			Date outwardDate = DATE.parse(bookingActionForm.getOutwardDate());

			Calendar returnCal = Calendar.getInstance();
			returnCal.setTime(returnDate);

			Calendar outwardCal = Calendar.getInstance();
			outwardCal.setTime(outwardDate);

			if (returnCal.get(Calendar.DAY_OF_WEEK) < outwardCal
					.get(Calendar.DAY_OF_WEEK)) {
				// weekend price
				result.setReturnPrice(bookingActionForm.getReturnPrice2());
				result.setOutwardPrice(bookingActionForm.getOutwardPrice2());
			} else {

				returnCal.add(Calendar.DAY_OF_YEAR, -7);
				if (returnCal.getTime().getTime() > outwardCal.getTime()
						.getTime()) {

					// weekend price
					result.setReturnPrice(bookingActionForm.getReturnPrice2());
					result
							.setOutwardPrice(bookingActionForm
									.getOutwardPrice2());
				} else {
					// weekday price
					result.setReturnPrice(bookingActionForm.getReturnPrice1());
					result
							.setOutwardPrice(bookingActionForm
									.getOutwardPrice1());
				}

			}
			
			if (result.getReturnPrice().trim().equals("")) {
				result.setReturnPrice("UNKNOWN");
			}
			
			if (result.getOutwardPrice().trim().equals("")) {
				result.setReturnPrice("UNKNOWN");
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
		}

		request.setAttribute("simpleBookingForm", result);
		return (mapping.findForward("success"));

	}

	public static void main(String[] args) {
		String dateStr = "8/06/1072";

		Date date;
		try {
			date = DATE.parse(dateStr);
			System.out.println(DATE.format(date));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
		}

	}
}